import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class Poltsa extends Frame {
	Frame nireFrame;
	eska eskaera;
	String izena;
	String pasahitza;
	public Poltsa(Frame parent, Boolean modal, eska esk, String i, String p) {
		nireFrame = parent;
		eskaera = esk;
		izena = i;
		pasahitza = p;
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		try {
			jbInit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void jbInit() throws Exception {
		final JFrame f = new JFrame();
		Container container = f.getContentPane();
		container.setLayout(null);
		((JPanel)container).setBorder(BorderFactory.createTitledBorder("Aukeratutako produktuak"));
		if (eskaera != null) {
			int[] josta = new int[20];
			josta = eskaera.jost();
			int lehena = 1;
			int[] kop = new int[20];
			int ijost = 1;
			// Eskatuko diren produktu guztiak pantailaratu
			for (int i=0; i<20; i++) {
				if (josta[i] == 0 && josta[i-1] != 0) {
					JLabel kantitatea = new JLabel("Kopurua: " + kop[ijost]);
					kantitatea.setBounds(130, 50 + (ijost-1)*40, 100, 25);
					f.add(kantitatea);
				}
				if (josta[i] != 0) {
					if (lehena == 2 && josta[i] == josta[i-1]) {
						kop[ijost]++;
					}
					if (lehena == 2 && josta[i] != josta[i-1]) { 
						JLabel kantitatea = new JLabel("Kopurua: " + kop[ijost]);
						kantitatea.setBounds(130, 50 + (ijost-1)*40, 100, 25);
						f.add(kantitatea);
						ijost++;
						if (josta[i] == 1) {
							JLabel jostailua = new JLabel("Kaxa magikoa");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}
						else if (josta[i] == 2) {
							JLabel jostailua = new JLabel("Ezpata");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}	
						else if (josta[i] == 3) {
							JLabel jostailua = new JLabel("Zaldia");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}
						else if (josta[i] == 4) {
							JLabel jostailua = new JLabel("Pistola");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}
						else if (josta[i] == 5) {
							JLabel jostailua = new JLabel("Panpina");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}
						else if (josta[i] == 6) {
							JLabel jostailua = new JLabel("Trizikloa");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}
						else if (josta[i] == 7) {
							JLabel jostailua = new JLabel("Baloia");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}
						else if (josta[i] == 8) {
							JLabel jostailua = new JLabel("Patinak");
							jostailua.setBounds(20, 50 + (ijost-1)*40, 100, 25);
							f.add(jostailua);
						}
						kop[ijost]++;
					}	
					if (lehena == 1) {
						if (josta[i] == 1) {
							JLabel jostailua1 = new JLabel("Kaxa magikoa");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}
						else if (josta[i] == 2) {
							JLabel jostailua1 = new JLabel("Ezpata");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}	
						else if (josta[i] == 3) {
							JLabel jostailua1 = new JLabel("Zaldia");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}
						else if (josta[i] == 4) {
							JLabel jostailua1 = new JLabel("Pistola");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}
						else if (josta[i] == 5) {
							JLabel jostailua1 = new JLabel("Panpina");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}
						else if (josta[i] == 6) {
							JLabel jostailua1 = new JLabel("Trizikloa");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}
						else if (josta[i] == 7) {
							JLabel jostailua1 = new JLabel("Baloia");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}
						else if (josta[i] == 8) {
							JLabel jostailua1 = new JLabel("Patinak");
							jostailua1.setBounds(20, 50, 100, 25);
							f.add(jostailua1);
						}
						lehena++;
						kop[ijost]++;
					}
				}
			}
			JLabel prezioa = new JLabel("Denera: " + eskaera.prezioa() + " euro");
			prezioa.setBounds(130, 50 + (ijost)*40, 100, 25);
			f.add(prezioa);
		}
		JButton eskaeral = new JButton("Eskaera egin");
		eskaeral.setBounds(20, 410, 120, 25);
		f.add(eskaeral);
		JButton hustu = new JButton("Poltsa hustu");
		hustu.setBounds(150, 410, 120, 25);
		f.add(hustu);
		JButton irten = new JButton("Irten");
		irten.setBounds(280, 410, 90, 25);
		f.add(irten);
		f.setSize(400,500);
		f.setTitle("Euskaltoy");
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		eskaeral.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Eskaera datu basean sartu
				AplikazioDatuBase datubase = AplikazioDatuBase.instance();
				datubase.SartuEskaera(eskaera);
				eskaera_actionPerformed(e);
				f.setVisible(false);
			}
		});
		hustu.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				hustu_actionPerformed(e);
				f.setVisible(false);
			}
		});
		irten.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				irten_actionPerformed(e);
				f.setVisible(false);
			}
		});
	}
	void eskaera_actionPerformed(ActionEvent e) {
		new EskaeraOna(this, true, izena, pasahitza);
	}
	void hustu_actionPerformed(ActionEvent e) {
		new Poltsa(this, true, null, izena, pasahitza);
	}
	void irten_actionPerformed(ActionEvent e) {
		new Frame1();
	}
}
